package com.wxy.school.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxy.school.common.R;
import com.wxy.school.dto.ShopDto;
import com.wxy.school.entity.Shop;
import com.wxy.school.service.ShopService;
import com.wxy.school.utils.BaseContext;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 王祥宇
 * @projectName school
 * @packageName com.wxy.school.controller
 * @data 2023/3/12 13:45
 * @description: TODO
 * @version: 1.0
 */

@RestController
@RequestMapping("school/shop")
@Slf4j
public class ShopController {

    @Resource
    private ShopService shopService;

    /**
     * @description: 获取商家列表
     * @author: 王祥宇
     * @param: pageNum
     * @param: pageSize
     * @param: qurey
     * @return: com.wxy.school.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     */
    @GetMapping("/getShopList")
    public R<Page> get(int pageNum, int pageSize, String qurey){
        Page<Shop> shopPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Shop> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(qurey), Shop::getShopName, qurey);
        lambdaQueryWrapper.orderByDesc(Shop::getCreateTime);
        shopService.page(shopPage, lambdaQueryWrapper);
        return R.success(shopPage);
    }

    /**
     * @description: 获取所有商家列表
     * @author: 王祥宇
     * @param: pageNum
     * @param: pageSize
     * @param: qurey
     * @return: com.wxy.school.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     */
    @GetMapping("/getAllShopList")
    public R<List<Shop>> getAll(String qurey){
        LambdaQueryWrapper<Shop> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Shop::getRate);
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(qurey), Shop::getShopName, qurey);
        List<Shop> shopList = shopService.list(lambdaQueryWrapper);
        if (shopList.isEmpty()){
            shopList = shopService.list();
        }
        return R.success(shopList);
    }

    /**
     * @description: 添加商家信息
     * @author: 王祥宇
     * @param: shopDto
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @PostMapping("/addShop")
    public R<String> add(@RequestBody ShopDto shopDto){
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopDto, shop, "userName");
        BaseContext.setThreadLocal(shopDto.getUserName());
        LambdaQueryWrapper<Shop> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Shop::getShopName, shopDto.getShopName());
        Shop shop1 = shopService.getOne(lambdaQueryWrapper);
        if (shop1 == null){
            shopService.save(shop);
            return R.success("商家信息添加成功");
        }else {
            return R.error("商家已存在");
        }
    }

    /**
     * @description: 修改商家信息
     * @author: 王祥宇
     * @param: shopDto
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @PutMapping("/updateShop")
    public R<String> update(@RequestBody ShopDto shopDto){
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopDto, shop, "userName");
        BaseContext.setThreadLocal(shopDto.getUserName());
        shopService.updateById(shop);
        return R.success("商家信息修改成功");
    }

    /**
     * @description: 修改商家点击量
     * @author: 王祥宇
     * @param: null
     * @return: null
     */
    @PutMapping("/updateRate/{id}")
    public R<String> updateRate(@PathVariable Long id) {
        Shop shop = shopService.getById(id);
        shop.setRate(shop.getRate() + 1);
        BaseContext.setThreadLocal(shop.getUpdateUser());
        shopService.updateById(shop);
        return R.success("修改成功");
    }

    /**
     * @description: 根据id进行查询
     * @author: 王祥宇
     * @param: id
     * @return: com.wxy.school.common.R<com.wxy.school.entity.Shop>
     */
    @GetMapping("/getById/{id}")
    public R<Shop> getById(@PathVariable Long id){
        LambdaQueryWrapper<Shop> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Shop::getId, id);
        Shop shop = shopService.getOne(lambdaQueryWrapper);
        if (shop == null){
            return R.error("该用户不存在");
        }else {
            return R.success(shop);
        }
    }

    /**
     * @description: 删除商家信息
     * @author: 王祥宇
     * @param: id
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @DeleteMapping("/deleteShop/{id}")
    public R<String> delete(@PathVariable Long id){
        shopService.removeById(id);
        return R.success("商家信息删除成功");
    }
}
